package com.atguigu.JVMDay03;

import java.util.ArrayList;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;

//写一个OOM的例子
public class OOMDemo {
    public static void main(String[] args) {
        ArrayList<byte[]> list = new ArrayList<>();
        while(true){
            System.out.println("当前堆内存大小为："+Runtime.getRuntime().totalMemory() / 1024/1024+"M");
            System.out.println("剩余堆内存大小为："+Runtime.getRuntime().freeMemory()/ 1024/1024+"M");

//            Dog dog = new Dog();
            byte[] bytes = new byte[5*1024*1024];
            list.add(bytes);
            //线程睡眠几秒
            try {
                TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
        System.gc();
        }



    }
}

class Dog{
    int age;
    String name;
    byte[] bytes = new byte[1*1024*1024];//1MB
    public Dog() {

    }

    @Override
    public String toString() {
        return "Dog{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
